<?php
/**
 * Classe de twitter
 * classe para resgatar dados postados por um usuario do twitter
 * @author Thiago Carvalho e Wiliame Morais @ PC4 Team
 */
class PC4_Util_Twitter{
	protected $screenName;
	protected $urlUserTimeline;
	
	public function __construct($screenName, $urlUserTimeline = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='){
		$this->screenName = $screenName;
		$this->urlUserTimeline = $urlUserTimeline;
	}
	
	public function setScreenName($screenName){
		$this->screenName = $screenName;
	}
	
	public function getTweets($numTweets = 20){
		$twiteesJson = $this->file_get_contents_curl($this->urlUserTimeline.$this->screenName);
		$twitees = json_decode($twiteesJson);
		$myTweets = array();
		
		for($i = 1; $i <= $numTweets; $i++){
		
			if(!empty($twitees[$i]->text)){
				$myTweets[$i] = ($twitees[$i]->text);
			}
			
		}
		
		return $myTweets;
	}
	
	public function getNumOfFollowers(){
		$userData = $this->getAnything(array('user'), 1);
		return $userData[1]['user']->followers_count;
	}
	
	/**
	 * @param array $dados (array com o nome dos campos requisitados)
	 * Ex.: $dados = array('created_at', 'id', 'text');
	 * @param int $numTweets (numero de tweets requisitados)
	 * @return devolve um array de tweets
	 */
	 public function getAnything($dados, $numLines = 20){
		$twiteesJson = $this->file_get_contents_curl($this->urlUserTimeline.$this->screenName);
		$twitees = json_decode($twiteesJson);
		$myTweets = array();
		
		for($i = 0; $i < $numLines; $i++){
			if(!empty($twitees[$i]->id)){
			
				foreach($dados as $dado){
					if($dado == 'created_at'){
						$twitees[$i]->$dado = $this->formateData($twitees[$i]->$dado);
					}
					$myTweets[$i][$dado] = $twitees[$i]->$dado;
				}
				unset($dado);
			}
		}
		
		return $myTweets;
	}
	
	public function linkOfTweet($idTweet, $screenName = ''){
		if(empty($screenName)){
			$screenName = $this->screenName;
		}
		$urlTweet = "http://twitter.com/".$screenName."/status/" . $idTweet;
		return $urlTweet;
	}
	
	/**
	 * Metodo que repoe todos os links e hashtags de um tweet
	 * 
	 * @param String $tweet
	 * @return String (tweet com links nos lugares corretor - hashtags e links)
	 */
	public function linkify_tweet($tweet) {
		$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
			'<a href="$1" target="_blank"><font color="#00B7EB">$1</font></a>',
			$tweet);
		$tweet = preg_replace('/(^|\s)@(\w+)/',
			'\1<a href="http://www.twitter.com/\2" target="_blank"><font color="#00B7EB">@\2</font></a>',
			$tweet);
		return preg_replace('/(^|\s)#(\w+)/',
			'\1<a href="http://search.twitter.com/search?q=%23\2" target="_blank"><font color="#00B7EB">#\2</font></a>',
			$tweet);
	}
	
	public function tweetDebug($tweets){
		echo '<pre>';
		print_r($tweets);
		echo '</pre>';
	}
	
	protected function formateData($date){
		$meses = array(
			"Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12"
		);
		$date = explode(" ", $date);
		$formatedDate = $date[2] . "/" . $meses[$date[1]] . "/" . $date[5] . " às " . $date[3];
		
		return $formatedDate;
	}
	
	protected function file_get_contents_curl($url){
		$ch = curl_init();
		// informar URL e outras funções ao CURL
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// Acessar a URL e retornar a saída
		$output = curl_exec($ch);
		// liberar
		curl_close($ch);
		return $output;
	}
	
}
?>